hdu 5222 Exploration(并查集+拓扑排序)

Exploration

Time Limit: 30000/15000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 668    Accepted Submission(s): 187


Problem Description
Miceren likes exploration and he found a huge labyrinth underground! 

This labyrinth has  N  caves and some tunnels connecting some pairs of caves. 

There are two types of tunnel, one type of them can be passed in only one direction and the other can be passed in two directions. Tunnels will collapse immediately after Miceren passing them. 

Now, Miceren wants to choose a cave as his start point and visit at least one other cave, finally get back to start point. 

As his friend, you must help him to determine whether a start point satisfing his request exists.
 

Input
The first line contains a single integer  T , indicating the number of test cases.

Each test case begins with three integers  N, M1, M2 , indicating the number of caves, the number of undirectional tunnels, the number of directional tunnels. 

The next  M1  lines contain the details of the undirectional tunnels. Each line contains two integers  u, v  meaning that there is a undirectional tunnel between  u, v . ( u  v

The next  M2  lines contain the details of the directional tunnels. Each line contains integers  u, v  meaning that there is a directional tunnel from  u  to  v . ( u  v )

T  is about 100.

1  N,M1,M2  1000000.

There may be some tunnels connect the same pair of caves.

The ratio of test cases with  N > 1000  is less than 5%.
 

Output
For each test queries, print the answer. If Miceren can do that, output "YES", otherwise "NO".
 

Sample Input
  
  
2 5 2 1 1 2 1 2 4 5 4 2 2 1 2 2 3 4 3 4 1
 

Sample Output
  
  
YES NO
Hint
If you need a larger stack size, please use #pragma comment(linker, "/STACK:102400000,102400000") and submit your solution using C++.
 




n个点 m1条无向边 m2条有向边  且每条边只能走一次 问能不能从一个点出发之后再回到这个点

就是在无向边有向边组成的混合图中找到一个环 

对于无向边 借助于并查集进行处理 如果两个点有共同的祖先 则这两个点之间有一条无向边 则可以构成一个环

对于有向边 借助于拓扑排序进行处理 从而判断是否有环

此外 还要记得在写并查集时。。。。那个fa[x]=find_fa(fa[x]) 因为这个tle好多次。。。

<span style="font-family:KaiTi_GB2312;font-size:18px;">#include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string.h>
#include <string>
#include <vector>
#include <queue>
#pragma comment(linker, "/STACK:102400000,102400000")

#define MEM(a,x) memset(a,x,sizeof a)
#define eps 1e-8
#define MOD 10009
#define MAXN 1000100
#define MAXM 100010
#define INF 99999999
#define ll __int64
#define bug cout<<"here"<<endl
#define fread freopen("ceshi.txt","r",stdin)
#define fwrite freopen("out.txt","w",stdout)

using namespace std;

int rd[MAXN],fa[MAXN];
vector<int> vec[MAXN];

void init(int n)
{
    for(int i=1;i<=n;i++)
    {
        fa[i]=i;
        rd[i]=0;
        vec[i].clear();
    }
}
int find_fa(int x)
{
    if(fa[x]!=x) return fa[x]=find_fa(fa[x]);//要加fa[x]=
    else return fa[x];
}
int topsort(int n)
{
    queue<int> que;
    for(int i=1;i<=n;i++)
    {
        if(rd[i]==0) que.push(i);
    }
    while(!que.empty())
    {
        int u=que.front(); que.pop();
        for(int i=0;i<vec[u].size();i++)
        {
            rd[vec[u][i]]--;
            if(rd[vec[u][i]]==0)
            {
                que.push(vec[u][i]);
            }
        }
    }
    for(int i=1;i<=n;i++)
    {
        if(rd[i]>0) return 1;
    }
    return 0;
}

int main()
{
//    fread;
    int tc;
    scanf("%d",&tc);
    while(tc--)
    {
        int n,m1,m2;
        scanf("%d%d%d",&n,&m1,&m2);
        init(n);
        int u,v,x,y;
        int flag=0;
        for(int i=0;i<m1;i++)
        {
            scanf("%d%d",&u,&v);
            x=find_fa(u);
            y=find_fa(v);
            if(x==y)
                flag=1;
            else fa[x]=y;
        }
        for(int i=0;i<m2;i++)
        {
            scanf("%d%d",&u,&v);
            x=find_fa(u);
            y=find_fa(v);
            if(x==y)
                flag=1;
            else
            {
                vec[u].push_back(v);
                rd[v]++;
            }
        }
        if(flag)
            printf("YES\n");
        else
        {
            if(topsort(n))
                printf("YES\n");
            else printf("NO\n");
        }
    }
    return 0;
}
</span>





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值